require 'iconv'
 
class EncodingConv
  def self.conv_hash(target, source, hash)
    new_hash = {}
    hash.each do |key, value|
      if value.instance_of? String
        new_hash[key] = Iconv.conv(target, source, value)
      else
        new_hash[key] = value
      end
    end
    new_hash
  end
  
  def self.conv_hashes(target, source, hashes)
    new_hashes = []
    hashes.each do |hash|
      new_hashes << conv_hash(target, source, hash)
    end
    new_hashes
  end
  
  def self.conv_mails(target, source, mails)
    #~ new_mails = []
    #~ mails.each do |mail|
      #~ new_mail = {}
      #~ new_mail[:title] = Iconv.conv(target, source, mail[:title])
      #~ new_mail[:body] = Iconv.conv(target, source, mail[:body])
      #~ new_mail[:from] = Iconv.conv(target, source, mail[:from])
      #~ new_mail[:date] = Iconv.conv(target, source, mail[:date])
      #~ new_mails << new_mail
    #~ end
    #~ new_mails
    conv_hashes(target, source, mails)
  end
  
  def self.conv_mailboxes(target, source, mailboxes)
    new_mailboxes = []
    mailboxes.each do |mailbox|
      new_mailboxes << {
        :username => Iconv.conv(target, source, mailbox[:username]),
        :type => Iconv.conv(target, source, mailbox[:type]),
        :state => Iconv.conv(target, source, mailbox[:state]),
        :mails => conv_mails(target, source, mailbox[:mails])
      }
    end
    new_mailboxes
  end
  
  #~ def self.conv_notices(target, source, notices)
    #~ new_notices = []
    #~ notices.each do |notice|
      #~ new_notices << {
        #~ :title => Iconv.conv(target, source, notice[:title]),
        #~ :body => Iconv.conv(target, source, notice[:body]),
        #~ :date => Iconv.conv(target, source, notice[:date])
      #~ }
    #~ end
    #~ new_notices
  #~ end
  
  #~ def self.conv_materials(target, source, materials)
    #~ new_materials = []
    #~ materials.each do |material|
      #~ new_materials << {
        #~ :title => Iconv.conv(target, source, material[:title]),
        #~ :description => Iconv.conv(target, source, material[:description]),
        #~ :date => Iconv.conv(target, source, material[:date])
      #~ }
    #~ end
    #~ new_materials
  #~ end
  
  #~ def self.conv_homeworks(target, source, homeworks)
    #~ new_homeworks = []
    #~ homeworks.each do |homework|
      #~ new_homeworks << {
        #~ :title => Iconv.conv(target, source, homework[:title]),
        #~ :date => Iconv.conv(target, source, homework[:date]),
        #~ :deadline => Iconv.conv(target, source, homework[:deadline]),
        #~ :handed => homework[:handed],
        #~ :overdue => homework[:overdue],
      #~ }
    #~ end
    #~ new_homeworks
  #~ end
  
  def self.conv_courses(target, source, courses)
    if courses.instance_of? String
      return Iconv.conv(target, source, courses)
    else
      new_courses = []
      courses.each do |course|
        new_courses << {
          :name => Iconv.conv(target, source, course[:name]),
          :state => Iconv.conv(target, source, course[:state]),
          :notices => conv_hashes(target, source, course[:notices]),
          :materials => conv_hashes(target, source, course[:materials]),
          :homeworks => conv_hashes(target, source, course[:homeworks])
        }
      end
      return new_courses
    end
  end
  
end
